package com.sangmo.fts.process.inventory.model;

import com.sangmo.boot.framework.data.entity.general.SoftDeleteEntity;
import com.sangmo.boot.framework.data.json.meta.ExtView;
import com.sangmo.boot.framework.data.json.meta.Formula;
import com.sangmo.boot.framework.data.json.meta.One;
import com.sangmo.boot.framework.plugin.dataview.def.meta.DataField;
import com.sangmo.boot.framework.plugin.dataview.def.meta.DataView;
import com.sangmo.fts.basic.model.Company;
import com.sangmo.fts.common.IConstants;
import com.sangmo.fts.process.inventory.calculator.IfLoanGoodsTotalAmountCalculator;
import com.sangmo.fts.process.inventory.calculator.IfLoanRepaymentAmountCalculator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Entity
@Table(name = "if_Loan")
@DataView
@ExtView
@Formula(value = "repaymentAmount", calc = IfLoanRepaymentAmountCalculator.class, groups = {"IfLoan.search","IfLoan.detail"})
//@Formula(value = "goodsTotalAmount", calc = IfLoanGoodsTotalAmountCalculator.class, groups = {"IfLoan.search","IfLoan.detail"})
@SQLDelete(
        sql = "UPDATE #{entityName} SET deleted = CURRENT_TIMESTAMP WHERE id = ?"
)
@SQLDeleteAll(
        sql = "UPDATE #{entityName} SET deleted = CURRENT_TIMESTAMP WHERE id = ?"
)
@Where(
        clause = "deleted = 0"
)
public class IfLoan extends SoftDeleteEntity {

    @DataField(value = IConstants.DATA_TYPE.LONG, required = true, index = true)
    private Long trainingId;

    @DataField(value = IConstants.DATA_TYPE.STRING_50,required = true, index = true, unique = true)
    private String applyCode;

    @DataField(value = IConstants.DATA_TYPE.STRING_50, index = true, unique = true)
    private String code;

    @One(value = "applyCompany", target = Company.class, fieldset = {"id", "name","manager","phone"})
    @DataField(value = IConstants.DATA_TYPE.LONG, required = true, index = true)
    private Long applyId;

    @DataField(value = IConstants.DATA_TYPE.LONG, required = true, index = true)
    private Long contractId;

    @DataField(value = IConstants.DATA_TYPE.LONG, required = true, index = true)
    private Long waybillId;

    @DataField(value = IConstants.DATA_TYPE.LONG, required = true, index = true)
    @One(value = "ifStock", target = IfStock.class, groups = {"IfLoan.detail"})
    private Long stockId;

    @One(value = "bank", target = Company.class, fieldset = {"id", "name"})
    @DataField(value = IConstants.DATA_TYPE.LONG, required = true, index = true)
    private Long bankId;

    @DataField(value = IConstants.DATA_TYPE.DECIMAL_POSITIVE_MONEY, required = true)
    private BigDecimal goodsTotalAmount;

    @DataField(value = IConstants.DATA_TYPE.DECIMAL_POSITIVE_MONEY, required = true)
    private BigDecimal amount;

    @DataField(value = IConstants.DATA_TYPE.DATE, required = true)
    private Date startTime;

    @DataField(value = IConstants.DATA_TYPE.DATE, required = true)
    private Date endTime;

    @DataField(value = IConstants.IF_LOAN_DATA_TYPE.LOAN_DURATION, required = true)
    private String duration;

    @DataField(required = true)
    private BigDecimal rate;

    @DataField(value = IConstants.IF_LOAN_DATA_TYPE.LOAN_STATE, required = true, index = true)
    private String state = States.PENDING;

    @DataField(index = true)
    private String issueState;

    private String auditMind;

    private Date auditTime;

    private Long auditBy;

    /** 贷款是否已结清 **/
    @DataField(index = true)
    private boolean settled;

    public Long getTrainingId() {
        return trainingId;
    }

    public void setTrainingId(Long trainingId) {
        this.trainingId = trainingId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Long getApplyId() {
        return applyId;
    }

    public void setApplyId(Long applyId) {
        this.applyId = applyId;
    }

    public Long getContractId() {
        return contractId;
    }

    public void setContractId(Long contractId) {
        this.contractId = contractId;
    }

    public Long getWaybillId() {
        return waybillId;
    }

    public void setWaybillId(Long waybillId) {
        this.waybillId = waybillId;
    }

    public Long getStockId() {
        return stockId;
    }

    public void setStockId(Long stockId) {
        this.stockId = stockId;
    }

    public Long getBankId() {
        return bankId;
    }

    public void setBankId(Long bankId) {
        this.bankId = bankId;
    }

    public BigDecimal getGoodsTotalAmount() {
        return goodsTotalAmount;
    }

    public void setGoodsTotalAmount(BigDecimal goodsTotalAmount) {
        this.goodsTotalAmount = goodsTotalAmount;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public void setRate(BigDecimal rate) {
        this.rate = rate;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getAuditMind() {
        return auditMind;
    }

    public void setAuditMind(String auditMind) {
        this.auditMind = auditMind;
    }

    public Date getAuditTime() {
        return auditTime;
    }

    public void setAuditTime(Date auditTime) {
        this.auditTime = auditTime;
    }

    public Long getAuditBy() {
        return auditBy;
    }

    public void setAuditBy(Long auditBy) {
        this.auditBy = auditBy;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getApplyCode() {
        return applyCode;
    }

    public void setApplyCode(String applyCode) {
        this.applyCode = applyCode;
    }

    public String getIssueState() {
        return issueState;
    }

    public void setIssueState(String issueState) {
        this.issueState = issueState;
    }

    public boolean isSettled() {
        return settled;
    }

    public void setSettled(boolean settled) {
        this.settled = settled;
    }

    public interface Duration {
        String ONE_MONTH = "ONE_MONTH"; //1个月
        String THREE_MONTH = "THREE_MONTH"; //3个月
        String SIX_MONTH = "SIX_MONTH"; //6个月
    }

    public interface States {
        String PENDING = "PENDING"; //待审核
        String CONFIRMED = "CONFIRMED"; //审核通过/贷放款
        String REJECTED = "REJECTED"; //已驳回
    }

    public interface IssueStates {
        String PENDING = "PENDING";
        String COMPLETED = "COMPLETED";
    }

    public static final Map<String, BigDecimal> rateMap = new HashMap<String, BigDecimal>(){
        {
            put("AAA-ONE_MONTH", new BigDecimal("0.06375"));
            put("AAA-THREE_MONTH", new BigDecimal("0.0663"));
            put("AAA-SIX_MONTH", new BigDecimal("0.068"));
            put("AA-ONE_MONTH", new BigDecimal("0.066"));
            put("AA-THREE_MONTH", new BigDecimal("0.06864"));
            put("AA-SIX_MONTH", new BigDecimal("0.0704"));
            put("A-ONE_MONTH", new BigDecimal("0.0675"));
            put("A-THREE_MONTH", new BigDecimal("0.0702"));
            put("A-SIX_MONTH", new BigDecimal("0.072"));
            put("BBB-ONE_MONTH", new BigDecimal("0.069"));
            put("BBB-THREE_MONTH", new BigDecimal("0.07176"));
            put("BBB-SIX_MONTH", new BigDecimal("0.0736"));
            put("BB-ONE_MONTH", new BigDecimal("0.072"));
            put("BB-THREE_MONTH", new BigDecimal("0.07488"));
            put("BB-SIX_MONTH", new BigDecimal("0.0768"));
            put("B-ONE_MONTH", new BigDecimal("0.075"));
            put("B-THREE_MONTH", new BigDecimal("0.078"));
            put("B-SIX_MONTH", new BigDecimal("0.08"));
            put("CCC-ONE_MONTH", new BigDecimal("0.07875"));
            put("CCC-THREE_MONTH", new BigDecimal("0.0819"));
            put("CCC-SIX_MONTH", new BigDecimal("0.084"));
            put("CC-ONE_MONTH", new BigDecimal("0.0825"));
            put("CC-THREE_MONTH", new BigDecimal("0.0858"));
            put("CC-SIX_MONTH", new BigDecimal("0.088"));
            put("C-ONE_MONTH", new BigDecimal("0.08625"));
            put("C-THREE_MONTH", new BigDecimal("0.0897"));
            put("C-SIX_MONTH", new BigDecimal("0.092"));
        }
    };

    public interface IIfLoanRepaymentAmountCalculator{
    }

    public interface IIfLoanGoodsTotalAmountCalculator {
    }
}
